<?php
/**
 * GoodsSku
 * Created by Veyron
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/2/7
 * Time: 15:55
 */

namespace app\common\model\mysql;

use think\Model;

class GoodsSku extends BaseModel
{
    /**
     * 一对一关联goods表（goods表中 id -> sku表 good_id）
     */
    public function goods(){
        return $this->hasOne(Goods::class, 'id', 'goods_id');
    }

    /**
     * 根据 goods_id获取sku信息
     * @param $goodId
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSkusByGoodId($goodId){
        $where = [
            'goods_id' => $goodId,
//            'status'   => config('status.mysql.table_normal')
        ];
        return $this->where($where)->select();
    }
    /**
     * 库存加操作
     * @param $id
     * @param $num
     * @return mixed
     */
    public function incStrock($id,$num){
        return $this->where('id', $id)->inc('stock', $num)->update();
    }
    /**
     * 库存减操作
     * @param $id
     * @param $num
     * @return mixed
     */
    public function decStrock($id,$num){
        return $this->where('id', $id)->dec('stock', $num)->update();
    }
}

